<template>
    <view class="content">
        <van-popup position="bottom" @click-overlay="closePopup" :show="show" round z-index="104"
            custom-style="width: 100%;background:#ffffff;overflow: hidden; ">
            <view class="popup-content">
                <view class="popup-main">
                    <scroll-view :refresher-enabled="false" class="scroll-content" scroll-y :lower-threshold="80"  show-scrollbar="false">
                        <view style="display: flex; justify-content: space-between;">
                            <view class="popupTitle"> {{ title }}</view>
                            <van-icon name="cross" color="#FFFFFF" size="15" @click="closePopup" />
                        </view>
                        <view class="cancelPopup-wrapper ">
                            <!-- <view class="popupTips">取消后无法恢复，优惠券、积分可退回</view> -->
                            <!-- <view class="popupSubtitle">请选择退款原因</view> -->
                            <view class="reason-list">
                                <view v-for="(item, index) in popupData" :key="index" class="reason-item"
                                    :class="{ 'active': item.isChecked }" @click="selectReason(item)">
                                    {{ item.desc }}
                                </view>
                            </view>
                        </view>
                    </scroll-view>
                    <van-toast id="van-toast" />
                </view>
                <view class="goodsAction nav_box">
                    <button type="warn" class="checkBtn" @click="submit">下一步</button>
                </view>
            </view>
        </van-popup>
    </view>
</template>

<script>
import Toast from '@/wxcomponents/vant/toast/toast'
import { queryCancelReasonApi } from '@/api/service'
export default {
    props: {
        show: {
            type: Boolean,
            default: () => false
        },
        reasonTypeCode: {
            type: String,
            default: ''
        }
    },
    data () {
        return {
            curS: 0,
            curSName: '',
            date: '',
            title: '取消原因',
            option: {},
            url: '',
            stepperValue: 1,
            popupData: [

                {
                    key: 1,
                    reasonType: '临时有事'
                },
                {
                    key: 2,
                    reasonType: '需求变更'
                },
                {
                    key: 3,
                    reasonType: '地址变更'
                },
                {
                    key: 4,
                    reasonType: '价格问题'
                },
                {
                    key: 5,
                    reasonType: '服务质量担忧'
                },
                {
                    key: 6,
                    reasonType: '服务提供方变更人员'
                }

            ],
            popupHeight: '300',

            isCancel: true,
            REASON_MAPPING :{
                '成分与描述不符': 'INGREDIENTS_NOT_MATCH_DESCRIPTION',
                '协商一致退款': 'CONSENSUS_REFUND',
                '发错货物': 'WRONG_GOODS',
                '质量问题': 'QUALITY_PROBLEM',
                '少件': 'MISSING_ITEMS',
                '其他': 'OTHER'
            },
            selectedReasonType:null
        }
    },

    computed: {
    },

    watch: {
        show: {
            immediate: true,
            handler(newVal) {
                console.log(newVal, 'newVal')
                if (newVal && !this.hasStaff) {
                    console.log(this.hasStaff)
                    this.getCancelReason()
                }
            }
        }
    },
    onLoad () {
        // if (this.popupBiaoshi===0) {
        //     this.title = '商品排序'
        // } else if (this.popupBiaoshi===2) {
        //     this.title = '价格区间'
        // }
    },
    onReady () {
        // this.popupData.forEach(ele => {
        //     this.$set(ele, 'isChecked', false)
        // })
    },

    methods: {
        // 选口味
        // onClickSort(val,e,item){
        //     console.log(val,e,item,'商品排序')
        //     this.curS=val
        //     this.curSName=item.text
        // },
        // 单选框
        // radioChange (e) {
        //     console.log('radio发生change事件，携带value值为：', e.detail.value)
        //     const REASON_MAPPING = {
        //     '成分与描述不符': 'INGREDIENTS_NOT_MATCH_DESCRIPTION',
        //     '协商一致退款': 'CONSENSUS_REFUND',
        //     '发错货物': 'WRONG_GOODS',
        //     '质量问题': 'QUALITY_PROBLEM',
        //     '少件': 'MISSING_ITEMS',
        //     '其他': 'OTHER'
        // };
        //     this.popupData.forEach((item, index) => {
        //         console.log(index, 'index',item, typeof (item.key), typeof (e.detail.value), Number(item.key) === Number(e.detail.value))
        //         if (Number(item.key) === Number(e.detail.value)) {
        //             item.isChecked = true
        //             this.selectedReasonType = REASON_MAPPING[item.text];
        //         } else {
        //             item.isChecked = false
        //         }
        //     })
        // },

        // 选择原因
        getCancelReason () {
            queryCancelReasonApi().then(res => {
                console.log(res)
                if (res.code === 200) {
                    this.popupData = res.data
                    this.popupData.forEach(ele => {
                        this.$set(ele, 'isChecked', ele.code == this.reasonTypeCode)
                    })
                }
            })
        },

        selectReason(item) {
            this.popupData.forEach(i => {
                i.isChecked = i.code === item.code
            });
            this.selectedReasonType = item
        },
        // 关闭弹窗
        closePopup () {
            this.isCancel = false
            this.$emit('closeReasonPopup', this.isCancel)
        },

        submit () {
            console.log(this.selectedReasonType)
            this.closePopup()
            this.$emit('next', this.selectedReasonType)

        }

    }

}
</script>
<style lang="scss" scoped>
// @import '@/styles/variables.scss';
// .content {
//     display: flex;
//     flex-direction: column;
//     box-sizing: border-box;
//     overflow-y: auto;
//     z-index: -2;
//     scrollbar-width: none;
//     /* 针对Firefox */
// }
// 滚动
.scroll-content {
    flex: 1;
    height: 1px;
}
::v-deep .van-button__text {
    display: flex !important;
    flex-direction: column !important;
}
// 提示框
.popup-content {
    padding: 48rpx;
    font-size: 12px;
    background: #ffffff;
    border-radius: 16px;
    overflow: hidden;
    // height: 50%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    // justify-content: space-between

    .popup-main {
        height: 100%;
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
    }

    .popupTitle {
        font-size: 20px;
        text-align: left;
        color: #111111;
        line-height: 20px;
    }

    .cancelPopup-wrapper {
        padding-top:48rpx;
        padding-bottom: 150rpx;
    }

    .popupTips{
        font-size: 12px;
        text-align: left;
        color: #666666;
        background-color: #F0F0F0;
        padding-top: 10px ;
        padding-bottom: 10px;
        padding-left: 15px;
    }
    // 副标题
    .popupSubtitle{
        font-size: 14px;
        text-align: left;
        color: #666666;
        line-height: 14px;
        margin-top: 15px;
        margin-bottom: 30px;
    }

    // 商品排序
    .sort {
        .popup_item {
            font-size: 14px;
            text-align: left;
            color: #111111;
            line-height: 14px;
            margin-bottom: 20px;

            &.active {
                font-weight: bolder;
                color: #FF6203;
            }
        }

        .popup_item:last-child {
            margin-bottom: 0px;
        }
    }
    .itemLeft{
        display: flex;
        align-items: center;
    }

    // 价格区间
    .price_range {
        display: flex;
        justify-content: space-around;
        align-items: center;
    }

    // 加入购物车按钮
    .goodsAction {
        margin-top: 22px;
        width: calc(100% - 48px);
        text-align: center;
        height: 70px;
    }

    .button {
        width: 100%;
        font-size: 16px;
        color: #ffffff;
        height: 44px;
        line-height: 44px;
        border-radius: 25px;
        border-style: initial !important;
        padding: 0 !important;
        background: #FF6203 !important;
    }
}
.reason-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;  // 设置间距
}

.reason-item {
    flex: 0 0 48%;  // 每行两个，留2%作为间隔
    border: 2rpx solid #E6E6E6;
    border-radius: 16rpx;
    padding: 20rpx;
    text-align: center;
    box-sizing: border-box;
    transition: all 0.3s;
    color: #333333;
    font-size: 32rpx;

    &.active {
        border-color: #FF6203;
        background-color: #fff7f2;
        color:#FF6203 ;
        font-size: 32rpx;
    }
}
.checkBtn {
    background-color: #FF6203;
    border: none;
    border-radius: 38rpx;
    font-size: 28rpx;
    text-align: center;
    color: #FFFFFF;
    height: 86rpx;
    line-height: 86rpx;

    width: 100%;
}

.l20 {
    margin-left: 20px;
}

.r13 {
    margin-right: 13px;
}

.r8 {
    margin-right: 8px;
}
</style>
